/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.io.InputStream;

import util.*;
import input.*;
import sound.*;
import graphics.*;
import graphics.ScreenManager;
import graphics.NullRepaintManager;
import javax.sound.sampled.AudioInputStream;


/**
 *
 * @author Yati
 */
public class MenuGameState implements GameState,ActionListener{
    
    private String changeState=null;
    
    private final int NUMBER_OF_CLOUDS=3;
    private final int NUMBER_OF_FRAMES=16;
    private final float MAX_CLOUD_SPEED=-0.05f;
    private final float MIN_CLOUD_SPEED=-0.01f;
    
    private Image[] cloudFrames;
    
    private Sprite[] clouds;
    
    private ScreenManager screen;
    private SoundManager soundManager;
    
    private Sound music;
    private AudioInputStream musicIS;
    private MusicManager musicManager;
    
    private JButton play,quit;
    private Image playIm,quitIm,bkImage;
    private String bkImageName;
    
    public MenuGameState(ScreenManager screen,MusicManager musicManger,SoundManager soundManager,String backgrndImageName){
        this.screen=screen;
        this.soundManager=soundManager;
        this.musicManager=musicManger;
        bkImageName=backgrndImageName;
        clouds=new Sprite[NUMBER_OF_CLOUDS];
        cloudFrames=new Image[NUMBER_OF_FRAMES];
    }
    
    public void start(InputManager inputManager){
        NullRepaintManager.install();
        inputManager.setCursor(Cursor.getDefaultCursor());
        
         JFrame frame =(JFrame) screen.getFullScreenWindow();
         Container contentPane = frame.getContentPane();
         
        if (contentPane instanceof JComponent) {
            ((JComponent)contentPane).setOpaque(false);
        }
        
        // add components to the screen's content pane
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        contentPane.add(play);
        contentPane.add(quit);

        // explicitly lay out components (needed on some systems)
        frame.validate();
        
        //soundManager.play(music,null,true);
        musicManager.play("sounds/music.wav", true);
        
    }
    
     public void stop(){
         musicManager.stop();
         //soundManager.stopThreads();
         //soundManager.close();
     }
        
    public String getName(){
        return "Menu";
    }
    
    /**
        Returns the name of a state to change to if this state is
        ready to change to another state, or null otherwise.
    */
    public String checkForStateChange(){
        return changeState;
    }
    
    public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        if(src==play){
            play.setVisible(false);
            quit.setVisible(false);
            changeState="Main";
        }
        else 
        if(src==quit)
            changeState=GameStateManager.EXIT_GAME;
        
    }
    
    public void loadResources(util.ResourceManager resourceManager){
        bkImage=resourceManager.loadImage("menu/"+bkImageName);
        playIm=resourceManager.loadImage("menu/play.png");
        quitIm=resourceManager.loadImage("menu/quit.png");
        play=createButton("Play","Start playing!",playIm);
        quit=createButton("Quit","Exit game",quitIm);
        for(int i=1;i<NUMBER_OF_FRAMES+1;i++){
            cloudFrames[i-1]=resourceManager.loadImage("CloudSprite"+i+".png");
        }
        
        for(int i=0;i<NUMBER_OF_CLOUDS;i++){
            clouds[i]=createCloudSprite();
            clouds[i].setX((float)(Math.random()*screen.getWidth()));
            clouds[i].setY((float)(Math.random()*screen.getHeight()));
            //clouds[i].setY(10f);
            clouds[i].setVelocityX((float)MIN_CLOUD_SPEED+(float)(Math.random()*MAX_CLOUD_SPEED));
        }
        
        music=resourceManager.loadSound("sounds/music.wav");
        musicIS=resourceManager.loadSoundAsStream("sounds/music.wav");
        
    }
    
    /**
        Creates a Swing JButton. The image used for the button is
        located at "../images/menu/" + name + ".png". The image is
        modified to create a "default" look (translucent) and a
        "pressed" look (moved down and to the right).
        <p>The button doesn't use Swing's look-and-feel and
        instead just uses the image.
    */
    public JButton createButton(String name, String toolTip,Image image) {

        // load the image
        //String imagePath = "images/menu/" + name + ".png";
        //ImageIcon iconRollover = new ImageIcon(imagePath);
        ImageIcon iconRollover=new ImageIcon(image);
        int w = iconRollover.getIconWidth();
        int h = iconRollover.getIconHeight();

        // get the cursor for this button
        Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

        // make translucent default image
        Image imageTran = screen.createCompatibleImage(w, h,
            Transparency.TRANSLUCENT);
        Graphics2D g = (Graphics2D)imageTran.getGraphics();
        Composite alpha = AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, .5f);
        g.setComposite(alpha);
        g.drawImage(iconRollover.getImage(), 0, 0, null);
        g.dispose();
        ImageIcon iconDefault = new ImageIcon(imageTran);

        // make a pressed image
        imageTran = screen.createCompatibleImage(w, h,
            Transparency.TRANSLUCENT);
        g = (Graphics2D)imageTran.getGraphics();
        g.drawImage(iconRollover.getImage(), 2, 2, null);
        g.dispose();
        ImageIcon iconPressed = new ImageIcon(imageTran);

        // create the button
        JButton button = new JButton();
        button.addActionListener(this);
        button.setIgnoreRepaint(true);
        button.setFocusable(false);
        button.setToolTipText(toolTip);
        button.setBorder(null);
        button.setContentAreaFilled(false);
        button.setCursor(cursor);
        button.setIcon(iconDefault);
        button.setRolloverIcon(iconRollover);
        button.setPressedIcon(iconPressed);

        return button;
    }
    
    public void draw(Graphics2D g){
        g.drawImage(bkImage,0,0,null);
        JFrame frame =screen.getFullScreenWindow();
        frame.getLayeredPane().paintComponents(g);
        //draw the clouds
        for(int i=0;i<clouds.length;i++){
            int x = Math.round(clouds[i].getX());
            int y = Math.round(clouds[i].getY());
            
            g.drawImage(clouds[i].getImage(), x, y,null);
        }
    }
    
    public void update(long elapsedTime){
        //update cloud sprites
        for(int i=0;i<clouds.length;i++){
            clouds[i].update(elapsedTime);
            if(clouds[i].getX()<-clouds[i].getWidth()){
                clouds[i].setX(screen.getWidth());
                clouds[i].setY((float)(Math.random()*screen.getHeight()));
            }
        }
        
    }
    
    public Sprite createCloudSprite(){
        Animation anim=new Animation();
        for(int i=0;i<16;i++){
            anim.addFrame(cloudFrames[i], 100);
        }
        
        return new Sprite(anim);
    }
    
    
}
